//
//  File.swift
//
//
//  Created by didi on 2022/9/13.
//

import MolecularObjects
import Vapor

public struct AccessGuardMiddleware: AsyncMiddleware {
    let permission: Permission

    public init(_ permission: Permission) {
        self.permission = permission
    }

    public func respond(to request: Request, chainingTo next: AsyncResponder) async throws -> Response {
        guard try await request.checkAccess(for: permission) else { throw Abort(.forbidden) }
        return try await next.respond(to: request)
    }
}
